home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / memory / memman2b / mem_demo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-19  |  3.6 KB  |  124 lines

  1. /***    Mem_Demo.cpp
  2.  
  3.         Demonstration of the BigArray Class, which is a "wrapper" around
  4.         the MeMMaN XMS/EMS/Virtual Memory manager.
  5.  
  6.         Illustrates the Use of BigArray to declare a large array of structures
  7.         in "Extra" memory, and demonstrates the operator overloads.
  8. ***/
  9.  
  10. extern "C" {
  11.     #include <string.h>
  12.     #include <stdio.h>
  13.     #include <dos.h>
  14. }
  15.  
  16. #include <iostream.h>
  17.  
  18. #include "bigarray.hpp"
  19.  
  20. /***    Define a new data type to be stored in the BigArray
  21. ***/
  22. typedef struct {
  23.     char name[80];
  24.     int  age;
  25. } MyType;
  26.  
  27. demo(char *Strat) {
  28.     int i;
  29.  
  30.     /***    Allocate an array of 16K of these, which is 1.28MB
  31.     ***/
  32.     BigArray Monster(16384L,sizeof(MyType),Strat);
  33.     BigArray Baby(128L,sizeof(MyType),Strat);
  34.  
  35.     /***    We *must* test if it succeeded
  36.     ***/
  37.     if (!Monster) {
  38.         cout << "Couldn't allocate the monster!" << endl;
  39.         return 0;
  40.     } else {
  41.         cout << "Monster allocated OK from " << Monster.Type() << " Memory" << endl;
  42.         cout << "Size of buffer = " << (16384L*sizeof(MyType)) << " bytes!!" << endl;
  43.     }
  44.  
  45.     /***    Test the second array we tried to allocate
  46.     ***/
  47.     if (!Baby && *Strat=='V') {
  48.         cout << "Couldn't allocate two buffers because this is the shareware demo" << endl;
  49.     }
  50.  
  51.     /***    Now write a few selected entries
  52.     ***/
  53.     for (i=0; i<16384; i+=64) {
  54.         sprintf( ((MyType *)Monster[i])->name,
  55.                   "This is entry %d in the BigArray Monster!!",i );
  56.  
  57.         ((MyType *)Monster[i])->age = i % 100; // Best not get carried away!
  58.     }
  59.  
  60.     /***    Now stream out a few characters from the array:
  61.     ***/
  62.  
  63.     Monster[0];    // Position the array
  64.  
  65.     for (i=0;i<128;i++) Monster++;   // Step back and
  66.     for (i=0;i<64;i++)  Monster--;   // forth a bit
  67.  
  68.     /***    Read from there 1 at a time
  69.     ***/
  70.     {
  71.         byte c;
  72.         while (-1!= (Monster >> c)) {
  73.             cout << c;
  74.             delay(50);
  75.             if (!c) break;
  76.         }
  77.         cout << endl;
  78.     }
  79.  
  80.     /***    Now stream a few into the array
  81.     ***/
  82.     Monster[32];    // Position the array to reset the offset
  83.     {
  84.         char *p="This is what I wrote into index 32!";
  85.  
  86.         do { Monster << *p; } while (*p++);
  87.     }
  88.  
  89.     /***    Write that out demonstrating the action of a plain *
  90.     ***/
  91.     cout << ((MyType *)*Monster)->name << endl;
  92.  
  93. }
  94.  
  95. main () {
  96.     /***    Note that you can usually accept the default allocation
  97.             strategy of "XECV" since this takes the best memory that
  98.             is available, trying each in turn, keeping conventional
  99.             free unless needed as a last resort.
  100.     ***/
  101.  
  102.     cout << "Trying conventional.....";
  103.     demo ("C");
  104.     cout << "           (Not that surprising, really)" << endl;
  105.     cout << "                -----------------------------------------------"<<endl;
  106.  
  107.     cout << "Trying XMS (Extended)...";
  108.     demo ("X");
  109.     cout << "           (Extended is the second fastest to conventional)" << endl;
  110.     cout << "                -----------------------------------------------"<<endl;
  111.  
  112.     cout << "Trying EMS (Expanded)...";
  113.     demo ("E");
  114.     cout << "           (Expanded has the extra overhead of paging)" << endl;
  115.     cout << "                -----------------------------------------------"<<endl;
  116.  
  117.     cout << "Trying Virtual (Disk)...";
  118.     demo ("V");
  119.     cout << "           (Virtual is super slow, but it's better than nothing!!)" << endl;
  120.     cout << "                -----------------------------------------------"<<endl;
  121.  
  122.     cout << "                    Yer dead flash or what! (English slang)" << endl;
  123. }
  124.